package org.yelongframework.image.resize;

import org.yelongframework.image.ImageWidthHeight;
import org.yelongframework.lang.Nullable;

/**
 * 图片调整属性
 * 
 * @since 1.0.0
 */
public class ImageResizeProperties {

	/** 调整后的宽度 */
	@Nullable
	private Integer width;

	/** 调整后的高度 */
	@Nullable
	private Integer height;

	/** 是否等比例缩放 */
	@Nullable
	private ImageResizeEqualProportion equalProportion;

	/** 输出的图片格式。如：jpg,png,jpeg */
	private String imageType = "jpg";

	// ==================================================构造器==================================================

	/**
	 * @param width  宽度
	 * @param height 高度
	 */
	public ImageResizeProperties(@Nullable Integer width, @Nullable Integer height) {
		this.width = width;
		this.height = height;
	}

	// ==================================================宽度==================================================

	/**
	 * @return 宽度
	 */
	@Nullable
	public Integer getWidth() {
		return width;
	}

	/**
	 * @param width 宽度
	 */
	public void setWidth(@Nullable Integer width) {
		this.width = width;
	}

	// ==================================================高度==================================================

	/**
	 * @return 高度
	 */
	public Integer getHeight() {
		return height;
	}

	/**
	 * @param height 高度
	 */
	public void setHeight(@Nullable Integer height) {
		this.height = height;
	}

	// ==================================================宽高==================================================

	/**
	 * @return 宽高
	 */
	@Nullable
	public ImageWidthHeight getWidthHeight() {
		if (null == width || null == height) {
			return null;
		}
		return new ImageWidthHeight(width, height);
	}

	/**
	 * @param width  宽度
	 * @param height 高度
	 */
	public void setWidthHeight(@Nullable Integer width, @Nullable Integer height) {
		this.width = width;
		this.height = height;
	}

	// ==================================================图片调整等比缩放==================================================

	/**
	 * @return 图片调整等比缩放
	 */
	@Nullable
	public ImageResizeEqualProportion getEqualProportion() {
		return equalProportion;
	}

	/**
	 * @param imageResizeEqualProportion 图片调整等比缩放
	 */
	public void setEqualProportion(@Nullable ImageResizeEqualProportion imageResizeEqualProportion) {
		this.equalProportion = imageResizeEqualProportion;
	}

	// ==================================================图片格式==================================================

	/**
	 * @return 图片格式
	 */
	public String getImageType() {
		return imageType;
	}

	/**
	 * @param imageType 图片格式
	 */
	public void setImageType(String imageType) {
		this.imageType = imageType;
	}

	// ==================================================调整至的宽高==================================================

	/**
	 * 获取需要调整到的图片宽高
	 * 
	 * @param sourceImageWidthHeight 源图片宽高
	 * @return 调整后的图片宽高
	 */
	public ImageWidthHeight getResizeImageWidthHeight(ImageWidthHeight sourceImageWidthHeight) {
		if (null == equalProportion) {
			validateWidth();
			validateHeight();
			return new ImageWidthHeight(width, height);
		}
		int sourceImageWidth = sourceImageWidthHeight.getWidth();
		int sourceImageHeigth = sourceImageWidthHeight.getHeight();
		switch (equalProportion) {
		case BY_WIDTH:
			validateWidth();
			return resizeByWidth(sourceImageWidth, sourceImageHeigth);
		case BY_HEIGHT:
			validateHeight();
			return resizeByHeight(sourceImageWidth, sourceImageHeigth);
		case AUTO:
			validateWidth();
			validateHeight();
			return resizeByAuto(sourceImageWidth, sourceImageHeigth);
		}
		throw new IllegalArgumentException();
	}

	/**
	 * 调整大小根据宽度
	 * 
	 * @param sourceImageWidth  源图片宽度
	 * @param sourceImageHeigth 源图片高度
	 * @return 图片宽高
	 */
	protected ImageWidthHeight resizeByWidth(int sourceImageWidth, int sourceImageHeigth) {
		int h = (int) (sourceImageHeigth * width / sourceImageWidth);
		return new ImageWidthHeight(width, h);
	}

	/**
	 * 调整大小根据高度
	 * 
	 * @param sourceImageWidth  源图片宽度
	 * @param sourceImageHeigth 源图片高度
	 * @return 图片宽高
	 */
	protected ImageWidthHeight resizeByHeight(int sourceImageWidth, int sourceImageHeigth) {
		int w = (int) (sourceImageWidth * height / sourceImageHeigth);
		return new ImageWidthHeight(w, height);
	}

	/**
	 * 调整大小根据自动
	 * 
	 * @param sourceImageWidth  源图片宽度
	 * @param sourceImageHeigth 源图片高度
	 * @return 图片宽高
	 */
	protected ImageWidthHeight resizeByAuto(int sourceImageWidth, int sourceImageHeigth) {
		if (sourceImageWidth / sourceImageHeigth > width / height) {
			return resizeByWidth(sourceImageWidth, sourceImageHeigth);
		} else {
			return resizeByHeight(sourceImageWidth, sourceImageHeigth);
		}
	}

	// ==================================================验证宽度==================================================

	/**
	 * 验证宽度
	 */
	public void validateWidth() {
		if (null == width) {
			throw new IllegalArgumentException("width must not be empty");
		}
		if (width < 0) {
			throw new IllegalArgumentException("width cannot be less than or equal to 0");
		}
	}

	// ==================================================验证高度==================================================

	/**
	 * 验证高度
	 */
	public void validateHeight() {
		if (null == height) {
			throw new IllegalArgumentException("height must not be empty");
		}
		if (height < 0) {
			throw new IllegalArgumentException("height cannot be less than or equal to 0");
		}
	}

}
